package com.lwc.backendsinglestructure.controller;


import com.lwc.backendsinglestructure.annotation.OperationLogInfo;
import com.lwc.backendsinglestructure.common.BaseResponse;
import com.lwc.backendsinglestructure.common.utils.ResultUtils;
import com.lwc.backendsinglestructure.domain.dto.user.CaptchasForm;
import com.lwc.backendsinglestructure.domain.dto.user.ForgetForm;
import com.lwc.backendsinglestructure.domain.dto.user.LoginForm;
import com.lwc.backendsinglestructure.domain.dto.user.RegisterForm;
import com.lwc.backendsinglestructure.domain.entity.User;
import com.lwc.backendsinglestructure.domain.vo.CaptchaVo;
import com.lwc.backendsinglestructure.service.UserService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户 控制层。
 *
 * @author <a href="https://gitee.com/liu-wenchan">程序员Rel</a>
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户注册
     *
     */
    @PostMapping("/register")
    public BaseResponse<Boolean> rgister(@RequestBody @Valid RegisterForm registerForm) {
        Boolean register = userService.register(registerForm);
        return ResultUtils.success(register);
    }

    /**
     * 用户登录
     *
     * @return 脱敏后的用户登录信息
     */
    @PostMapping("/login")
    public BaseResponse<User> login(@RequestBody LoginForm loginForm) {
        User user = userService.login(loginForm);
        return ResultUtils.success(user);
    }

    /**
     * 忘记密码
     * @return
     */
    @PostMapping("/forget")
    public BaseResponse<?> forget(@RequestBody ForgetForm forgetForm) {
        Boolean forget = userService.forget(forgetForm);
        return ResultUtils.success(forget);
    }


    /**
     * 发送验证码
     * @return
     */
    @PostMapping("/sendCaptchas")
    public BaseResponse<?> sendCaptchas(@RequestBody @Valid CaptchasForm captchasForm) {
        userService.sendCaptchas(captchasForm);
        return ResultUtils.success(null);
    }


    /**
     * 获取图像验证码
     * 前端携带计算结果 以及 uuid请求后端获取redis的存储结果校验即可
     * @return
     */
    @OperationLogInfo(module = "用户模块", api = "/user/getCaptchas", title = "获取图像验证码", method = "POST",description = "获取图形验证码desc")
    @PostMapping("/getCaptchas")
    public BaseResponse<CaptchaVo> getCaptchas() {
        CaptchaVo captchas = userService.getCaptchas();
        return ResultUtils.success(captchas);
    }



}
